//
//  HMEmoticonKeyboard.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/30.
//  Copyright © 2016年 itcast. All rights reserved.
//  表情键盘 显示页面

import UIKit

class HMEmoticonKeyboard: UIView {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置ui
    
    private func setupUI(){
        
        // 点击表情类型按钮要执行的闭包
        toolBar.emoticonButtonClickClosure = { (type)->() in
            print(type)
//            让pagecontrol随着翻页变动
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            // 设置pageControl的数据显示
            self.showPageControl(indexPath)
        }
        //背景颜色
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
//        backgroundColor = UIColor.redColor()
        //添加子控件
        //底部按钮
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        //设置约束
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        print(toolBar.frame.size.height)
        }
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar.snp_top)
            make.centerX.equalTo(toolBar)
        }
        
        // 需要在界面完成展示出来之后再去移动（collectionView大小已经确定，已经走过一遍数据源方法/ 主线程空闲的时候）
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            let indexPath = NSIndexPath(forItem: 0, inSection: 1)
            
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            // 选中底部的按钮
            self.toolBar.selectSection = indexPath.section
            // 设置pageControl显示的内容
            self.showPageControl(indexPath)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: SCREENW, height: self.frame.height - toolBar.frame.height)
    }
    
    
   
    
    /**
    *  懒加载控件
    */
     

     /// toolBar
    private lazy var toolBar: HMEmoticonToolBar = HMEmoticonToolBar(frame: CGRectZero)
    
    
    private lazy var emoticonCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        // layout.itemSize = CGSize(width: SCREENW, height: 216-37)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 设置布局方向
        layout.scrollDirection = .Horizontal
        
        let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clearColor()
        // 注册cell
        collectionView.registerClass(HMEmoticonPageCell.self, forCellWithReuseIdentifier: "cell")
        // 设置数据源以及代理
        collectionView.dataSource = self
//        collectionView.delegate = self
        // 去掉弹簧效果
        collectionView.bounces = false
        // 分页
        collectionView.pagingEnabled = true
        // 隐藏滚动条
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    
    // 分页控件
    private lazy var pageControl: UIPageControl = {
        let control = UIPageControl()
        control.hidden = true
        //        control.currentPageIndicatorTintColor = UIColor.orangeColor()
        //        control.pageIndicatorTintColor = UIColor.blackColor()
        // 设置不同状态的图片
        //        control.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        //        control.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        
        control.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        control.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        // 关闭用户交互
        control.userInteractionEnabled = false
        return control
    }()
    
    
}

extension HMEmoticonKeyboard: UICollectionViewDataSource {
    
    
    /// 返回多少组 --> 有多少种表情
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return HMEmoticonKeyBoardViewModel.sharedViewModel.allEmoticons.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HMEmoticonKeyBoardViewModel.sharedViewModel.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//        // 从缓存中取
//        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
//        
//        cell.backgroundColor = RandomColor
//        cell.ssss = allEmoticon[indexPath.section][indexPath.item]; 
        // 从缓存中取
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! HMEmoticonPageCell
        cell.indexPath = indexPath
        
        // 设置每一个cell的数组
        cell.emoticons = HMEmoticonKeyBoardViewModel.sharedViewModel.allEmoticons[indexPath.section][indexPath.item]
        
        return cell
    }
    
    /// 设置pageControl显示的内容
    ///
    /// - parameter indexPath: <#indexPath description#>
    private func showPageControl(indexPath: NSIndexPath) {
        // 1. 更改PageControl 的总页数
        pageControl.numberOfPages = HMEmoticonKeyBoardViewModel.sharedViewModel.allEmoticons[indexPath.section].count
        pageControl.currentPage = indexPath.item
        // 如果是最近表情，不显示pageControl
        pageControl.hidden = indexPath.section == 0
    }
}

